package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dao.ProductInfoDao;
import com.example.demo.entity.ProductInfo;
import com.example.demo.service.ProductInfoService;
import org.springframework.stereotype.Service;

/**
 * 产品信息表(ProductInfo)表服务实现类
 *
 * @author hzhang
 * @since 2023-02-09 11:33:21
 */
@Service("productInfoService")
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoDao, ProductInfo>
        implements ProductInfoService {

    @Override
    public void reWorkInc(ProductInfo pi) {
        QueryWrapper<ProductInfo> wpi = new QueryWrapper<>();
        // 设置条件
        wpi.eq("product_id", pi.getProductId())
                .eq("order_code", pi.getOrderCode());

        // 查询数据并获取一条
        ProductInfo p = this.getOne(wpi);

        if (null == p) {
            // 数据库不存在时插入
            pi.setReworkNum(1);
            pi.insert();
        } else {
            // 数据库存在时 reworkNum + 1
            p.setReworkNum(p.getReworkNum() + 1);
            p.update(wpi);
        }
    }
}

